package com.mf.ergate.web.controller.opt;

import com.mf.ergate.api.auth.model.dto.RoleInfoQueryDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationCreateDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationDeleteDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationQueryDto;
import com.mf.ergate.api.auth.model.vo.RoleInfoVo;
import com.mf.ergate.api.auth.model.vo.RoleUserRelationVo;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoCreateDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoDeleteDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoQueryDto;
import com.mf.ergate.api.opt.model.dto.EmployeeInfoUpdateDto;
import com.mf.ergate.api.opt.model.vo.EmployeeInfoVo;
import com.mf.ergate.web.common.utils.MfParamUtils;
import com.mf.ergate.web.domain.enums.auth.RoleTypeEnum;
import com.mf.ergate.web.domain.enums.generic.GenderEnum;
import com.mf.ergate.web.domain.enums.generic.IsEnabledEnum;
import com.mf.ergate.web.service.opt.EmployeeInfoService;
import com.mf.framework.common.model.Page;
import org.apache.commons.codec.digest.Md5Crypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工信息Controller
 * User: zhaoming
 * DateTime: 2019-04-01
 * To change XkParamUtils template use File | Settings | File Templates.
 */
@Secured({"MANAGE_OPT_EMPLOYEE_INFO"})
@Controller
@RequestMapping("/web/employeeInfo")
public class EmployeeInfoController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Value("${salt}")
    private String salt;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("opt/employee/employee_info_index");
        modelAndView.addObject("orgInfos", employeeInfoService.getOrgInfos());
        return modelAndView;
    }

    /**
     * 新增页面
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("opt/employee/employee_info_add");
        modelAndView.addObject("orgInfos", employeeInfoService.getOrgInfos());
        modelAndView.addObject("GenderEnum", GenderEnum.getList());
        return modelAndView;
    }

    /**
     * 维护页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/employee/employee_info_edit");
        modelAndView.addObject("uniqueNo", uniqueNo);
        modelAndView.addObject("orgInfos", employeeInfoService.getOrgInfos());
        modelAndView.addObject("GenderEnum", GenderEnum.getList());
        return modelAndView;
    }

    /**
     * 角色配置页面
     *
     * @param uniqueNo 用户编号
     * @return 模型视图
     */
    @RequestMapping(value = "/roleConfigPage", method = RequestMethod.GET)
    public ModelAndView roleConfigPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/employee/employee_info_config_role");
        modelAndView.addObject("employeeInfo", employeeInfoService.selectByUniqueNo(uniqueNo));
        return modelAndView;
    }

    /**
     * 修改密码页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPasswordPage", method = RequestMethod.GET)
    public ModelAndView editPasswordPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/employee/employee_info_edit_password");
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 新增
     * @param employeeInfoCreateDto 员工信息新增Dto
     * @param file 员工头像
     * @return 员工信息
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public EmployeeInfoVo insert(EmployeeInfoCreateDto employeeInfoCreateDto, MultipartFile file) {
        employeeInfoCreateDto.setEmployeePassword(Md5Crypt.md5Crypt(employeeInfoCreateDto.getEmployeePassword().getBytes(), salt));
        MfParamUtils.buildInsertEntity(employeeInfoCreateDto);
        return employeeInfoService.insert(employeeInfoCreateDto, file);
    }

    /**
     * 更新
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @param file 员工头像
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean update(EmployeeInfoUpdateDto employeeInfoUpdateDto, MultipartFile file) {
        MfParamUtils.buildUpdateEntity(employeeInfoUpdateDto);
        return employeeInfoService.updateByUniqueNo(employeeInfoUpdateDto, file);
    }

    /**
     * 修改密码
     *
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean updatePassword(EmployeeInfoUpdateDto employeeInfoUpdateDto) {
        // 加密密码
        employeeInfoUpdateDto.setEmployeePassword(Md5Crypt.md5Crypt(employeeInfoUpdateDto.getEmployeePassword().getBytes(), salt));
        MfParamUtils.buildUpdateEntity(employeeInfoUpdateDto);
        return employeeInfoService.updateByUniqueNo(employeeInfoUpdateDto, null);
    }

    /**
     * 根据主键删除(逻辑删除)
     * @param employeeInfoDeleteDto 员工信息删除Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(EmployeeInfoDeleteDto employeeInfoDeleteDto){
        MfParamUtils.buildUpdateEntity(employeeInfoDeleteDto);
        return employeeInfoService.deleteCompleteByUniqueNo(employeeInfoDeleteDto);
    }

    /**
     * 根据业务主键查询
     * @param uniqueNo 唯一编号
     * @return 员工信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public EmployeeInfoVo detail(String uniqueNo) {
        return employeeInfoService.selectByUniqueNo(uniqueNo);
    }

    /**
     * 根据条件查询
     * @param employeeInfoQueryDto 员工信息查询Dto
     * @return 员工信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<EmployeeInfoVo> list(EmployeeInfoQueryDto employeeInfoQueryDto) {
        return employeeInfoService.selectByCondition(employeeInfoQueryDto);
    }

    /**
     * 根据条件分页查询
     * @param employeeInfoQueryDto 员工信息查询Dto
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 员工信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<EmployeeInfoVo> page(EmployeeInfoQueryDto employeeInfoQueryDto,
                                    @RequestParam(value = "page") int pageNum,
                                    @RequestParam(value = "rows") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return employeeInfoService.selectPage(page, employeeInfoQueryDto);
    }

    /**
     * 检查重复数据
     *
     * @param employeeInfoQueryDto 员工信息查询Dto
     * @return 验证结果
     */
    @RequestMapping(value = "/checkRepeat", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkRepeat(EmployeeInfoQueryDto employeeInfoQueryDto) {
        Map<String, Object> map = new HashMap<>();
        if (employeeInfoService.selectCount(employeeInfoQueryDto) > 0) {
            map.put("error", "员工账号已被占用");
        }
        return map;
    }

    /**
     * 角色分页查询
     * @param roleInfoQueryDto 角色信息查询Dto
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 用户角色关联信息分页列表
     */
    @RequestMapping(value = "/rolePage", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<RoleInfoVo> rolePage(RoleInfoQueryDto roleInfoQueryDto,
                                     @RequestParam(value = "page") int pageNum,
                                     @RequestParam(value = "rows") int pageSize,
                                     @RequestParam(value = "sidx") String sortName,
                                     @RequestParam(value = "sord") String sortOrder) {
        roleInfoQueryDto.setRoleTypeCode(RoleTypeEnum.ROLE_OPERATE.getIndex());
        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return employeeInfoService.rolePage(page, roleInfoQueryDto);
    }

    /**
     * 新增用户与角色关联信息
     *
     * @param roleUserRelationCreateDto 用户与角色关联新增Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/insertRoleUserRelation", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public RoleUserRelationVo insertRoleUserRelation(RoleUserRelationCreateDto roleUserRelationCreateDto) {
        MfParamUtils.buildInsertEntity(roleUserRelationCreateDto);
        return employeeInfoService.insertRoleUserRelation(roleUserRelationCreateDto);
    }

    /**
     * 角色与用户关联分页查询
     * @param roleUserRelationQueryDto 用户与角色关联查询Dto
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 用户角色关联信息分页列表
     */
    @RequestMapping(value = "/roleUserRelationPage", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<RoleUserRelationVo> roleUserRelationPage(RoleUserRelationQueryDto roleUserRelationQueryDto,
                                                         @RequestParam(value = "page") int pageNum,
                                                         @RequestParam(value = "rows") int pageSize,
                                                         @RequestParam(value = "sidx") String sortName,
                                                         @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return employeeInfoService.roleUserRelationPage(page, roleUserRelationQueryDto);
    }

    /**
     * 删除用户与角色关联信息
     *
     * @param roleUserRelationDeleteDto 用户与角色关联删除Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteRoleUserRelation", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteRoleUserRelation(RoleUserRelationDeleteDto roleUserRelationDeleteDto) {
        MfParamUtils.buildUpdateEntity(roleUserRelationDeleteDto);
        return employeeInfoService.deleteRoleUserRelation(roleUserRelationDeleteDto);
    }

    /**
     * 修改配送权限
     *
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/updateDeliveryAuth", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean updateDeliveryAuth(EmployeeInfoUpdateDto employeeInfoUpdateDto) {
        // 加密密码
        MfParamUtils.buildUpdateEntity(employeeInfoUpdateDto);
        return employeeInfoService.updateByUniqueNo(employeeInfoUpdateDto, null);
    }

    /**
     * 启用
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/enableByUniqueNo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean enableByUniqueNo(EmployeeInfoUpdateDto employeeInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(employeeInfoUpdateDto);
        employeeInfoUpdateDto.setIsEnable(IsEnabledEnum.YES.getIndex());
        return employeeInfoService.updateByUniqueNo(employeeInfoUpdateDto, null);
    }

    /**
     * 停用
     * @param employeeInfoUpdateDto 员工信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/disableByUniqueNo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean disableByUniqueNo(EmployeeInfoUpdateDto employeeInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(employeeInfoUpdateDto);
        employeeInfoUpdateDto.setIsEnable(IsEnabledEnum.NO.getIndex());
        return employeeInfoService.updateByUniqueNo(employeeInfoUpdateDto, null);
    }
}